<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
</body>
<script>
    class Subscript{
        constructor(){
            this.events = {
                // "lw-event": [fn],
                // "lw-event": [fn,fn,fn],
            };
        }
        on(type,callback){
            if(this.events[type]){
                this.events[type].push(callback);
                this.events[type] = [...new Set(this.events[type])];
            }else{
                this.events[type] = [callback];
            }
        }
        off(type,callback){
            if(this.events[type]){
                var index = this.events[type].indexOf(callback);
                if(index != -1){
                    this.events[type].splice(index,1);
                }
            }
        }
        emit(type, data){
            if(this.events[type]){
                this.events[type].forEach((callback)=>{
                    callback(data);
                })
            }
        }
    }


    let callback1 = function(arg){
        console.log(1111, this);
        console.log("老王更新了通知我1",arg);
    }
    let callback2 = function(arg){
        console.log("老王更新了通知我2",arg);
    }

    var evt = new Subscript();
    console.log(evt);

    // 订阅
    evt.on("lw-event",callback1);
    evt.on("lw-event",callback2);

    // 取消订阅
    // evt.off("lw-event",callback1);
    // evt.off("lw-event",callback2);

    // 发布
    evt.emit("lw-event","更新视频");
    evt.emit("lw-event","更新Vue");



</script>
</html>